// better rand function that takes min/max and integer between them, much like Random.Next in .NET
Math.rand = function(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
}

// a nice trick to make it easier to pick random items from an array
Array.prototype.random = function() {
    return this[Math.rand(0, this.length)];
}

// basic clamp function
Math.clamp = function(val, min, max) {
    return Math.min(Math.max(val, min), max);
};

isInArea = function(x0, y0, x1, y1, radius) {
	if (radius>0) {
		var dist = Math.sqrt(Math.pow(x1-x0, 2)+Math.pow(y1-y0, 2));
		return dist <= radius;
	}
	else
		return true;		
};

var ZERO = 0.001;
var SMALL = 0.1;
isZero = function(A) {
	return ( (A < ZERO) && (A > -ZERO) ); 
};
isSmall = function(A) {
	return ( (A < SMALL) && (A > -SMALL) ); 
};
isSame = function(A, B) {
	return ( ((A-B) < ZERO) && ((A-B) > -ZERO) ); 
};
isSimilar = function(A, B) {
	return ( ((A-B) < SMALL) && ((A-B) > -SMALL) ); 
};
isBetween = function(A, B, C) {
	return ( ((A-B) > -ZERO) && ((A-C) < ZERO) ); 
};

// helper to map key values to names
var Keys = { Left: 37, Up: 38, Right: 39, Down: 40, Attack: 65, Defense: 68, Healt: 72, Map: 77};